//
//  LeftViewController.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/27.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

#import "LeftViewController.h"
#import "NightMode.h"
#import "NightSetTableViewCell.h"
#import "MainViewController.h"
#import "CollectViewController.h"
#import "AppDelegate.h"

@interface LeftViewController () <UITableViewDataSource,UITableViewDelegate,NightSetTableViewCellDelegate>
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic,retain) UIVisualEffectView * visual;
@end

@implementation LeftViewController
- (void)dealloc
{
    [_table release];
    [_tableArray release];
    [_visual release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        [_tableArray addObject:@"FASHION"];
        [_tableArray addObject:@"我的收藏"];
        [_tableArray addObject:@"夜间模式"];
        [_tableArray addObject:@"清除缓存"];
        [_tableArray addObject:@"关于我们"];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
    [self createVisual];
    [self createTable];
}
-(void)createVisual{
    // 添加毛玻璃
    self.visual = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle: [NightMode nightModeON]?UIBlurEffectStyleDark:UIBlurEffectStyleLight]];
    _visual.frame = [[UIScreen mainScreen] bounds];
    UIImage * backImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"back":@"back_light" ofType:@"png"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backImage];
    [self.view addSubview:_visual];
    [_visual release];
}
- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width /2+20, self.view.frame.size.height) style:UITableViewStylePlain];
    
    [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellIdentify"];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone; // 分割线样式，可以设置为none
    _table.alpha = 0.8;
    _table.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0]; // 把背景色设置为透明色，只显示self.view的背景色
    _table.showsVerticalScrollIndicator = NO;
    [_table setDelegate:self];
    [_table setDataSource:self];
    [self.view addSubview:_table];
    [_table release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentify"];
    cell.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];// 把背景色设置为透明色，只显示self.view的背景色
    cell.textLabel.text = [_tableArray objectAtIndex:indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    if(indexPath.row == 0){
        cell.textLabel.textColor = [NightMode nightModeON]?[UIColor lightGrayColor]:[UIColor blackColor];
    } else if (indexPath.row == 2) {
        NightSetTableViewCell * cell = [[NightSetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; // 不可以用重用池，会导致无法刷新字体颜色
//        cell.textLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.userInteractionEnabled = YES;
        return cell;

    }else
        cell.textLabel.textColor = [NightMode nightModeON]?[UIColor lightGrayColor]:[UIColor blackColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 选中哪个后就把他的字体变为白色，把其他的变为灰色
//    for (UITableViewCell * cell in [tableView visibleCells]) {
//        cell.textLabel.textColor = [NightMode nightModeON]?[UIColor lightGrayColor]:[UIColor darkGrayColor];
//    }
//    UITableViewCell * currentCell = [tableView cellForRowAtIndexPath:indexPath];
//    currentCell.textLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor cyanColor];
    YRSideViewController *sideViewController=[((AppDelegate*)[[UIApplication sharedApplication]delegate])sliderview];
    if (indexPath.row == 0) {
        MainViewController *main = [[MainViewController alloc] init];
        
        
        
        [self.navigationController pushViewController:main animated:YES];
        [sideViewController setRootViewController:main];
        [sideViewController hideSideViewController:YES];
    }
    
    if (indexPath.row == 1) {
        
        CollectViewController *collect = [[CollectViewController alloc] init];
        
        
        [self.navigationController pushViewController:collect animated:YES];
        
        [sideViewController setRootViewController:collect];
        [sideViewController hideSideViewController:YES];
        
        [collect release];
        
    }
    
    if (indexPath.row == 3) {
        NSFileManager * manager = [NSFileManager defaultManager];
        NSString * libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
        
        // 只删除自己创建的文件夹和SDWebImage图片缓存文件夹
        NSString * dailyPath = [NSString stringWithFormat:@"%@/%@",libraryPath,@"daily"];
        NSString * newsPath = [NSString stringWithFormat:@"%@/%@",libraryPath,@"news"];
        NSString * programaPath = [NSString stringWithFormat:@"%@/%@",libraryPath,@"programa"];
        NSString * SDWebImagePath = [NSString stringWithFormat:@"%@/%@",libraryPath,@"Caches/com.hackemist.SDWebImageCache.default"];
        [manager removeItemAtPath:dailyPath error:nil];
        [manager removeItemAtPath:newsPath error:nil];
        [manager removeItemAtPath:programaPath error:nil];
        [manager removeItemAtPath:SDWebImagePath error:nil];
        
        
        // 创建提示label
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
        label.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-35);
        label.text = @"清除成功";
        label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        label.textColor = [UIColor whiteColor];
        label.layer.cornerRadius = 10;
        label.clipsToBounds = YES; // 设置这个才会使圆角生效
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:17];
        [self.view addSubview:label];
        // 1s后让label消失
        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(labelDisappear:) userInfo:@{@"label":label} repeats:NO];
        [label release];
    }
    
    // 点击之后在0.5s的scrollView动画之内不能在点击，防止暴力测试
    _table.userInteractionEnabled = NO;
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(tableCanClick) userInfo:nil repeats:NO];
}

// 让提示label消失
-(void)labelDisappear:(NSTimer *)timer{
    UILabel * label = [timer.userInfo objectForKey:@"label"];
    [label removeFromSuperview];
}

-(void)tableCanClick{
    _table.userInteractionEnabled = YES;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (self.view.frame.size.height - 200)/_tableArray.count;
}
#pragma  mark - 当试图将要出现时，重新刷新self.view和tableView
-(void)viewWillAppear:(BOOL)animated{
    [self.view setNeedsDisplay];
    
    // 重新添加毛玻璃
    [_visual removeFromSuperview];
    self.visual = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle: [NightMode nightModeON]?UIBlurEffectStyleDark:UIBlurEffectStyleLight]];
    _visual.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:_visual];
    [_visual release];
    [self.view addSubview:_table];
    
    [_table reloadData]; // tableView不可以通过setNeedsDisplay设置，需要reloadData
    
    
}

#pragma mark - NightSetTableViewCellDelegate 设置夜间模式后立刻调整UI
-(void)changeNightMode{
    [self.table reloadData];
    
    self.tabBarController.tabBar.barStyle = [NightMode nightModeON]?UIBarStyleBlack:UIBarStyleDefault;
    
    self.navigationController.navigationBar.barStyle = [NightMode nightModeON]?UIBarStyleBlack:UIBarStyleDefault;
    
    // 重新添加毛玻璃
    [_visual removeFromSuperview];
    self.visual = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle: [NightMode nightModeON]?UIBlurEffectStyleDark:UIBlurEffectStyleLight]];
    _visual.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:_visual];
    [_visual release];
    [self.view addSubview:_table];
    
    [_table reloadData]; // tableView不可以通过setNeedsDisplay设置，需要reloadData
    [self.delegate leftViewClickWithRow];
    
    
    
    
    
    UIImage * backImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"back":@"back_light" ofType:@"png"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backImage];
}

@end
